package com.cloud.netty.tcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.Charset;

/**
 * @program: netty
 * @description:
 * @author: Cloud
 * @create: 2022/4/14 14:25:10
 */
public class ServerHandler extends SimpleChannelInboundHandler<ByteBuf> {


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        byte bt[] = new byte[msg.readableBytes()];
        msg.readBytes(bt);
        String string = new String(bt, Charset.forName("utf-8"));
        System.out.println("接收到数据,数据为: "+string);
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
